package com.moyu.shopapp.controller;

import com.moyu.shopapp.entity.User;
import com.moyu.shopapp.service.UserService;
import com.moyu.shopapp.service.impl.UserServiceImpl;
import com.moyu.shopapp.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查找所有用户
     * @return
     */
    @GetMapping("/findAllUser")
    public List<User> users(){
        System.out.println(userService.list());
       return userService.list();
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public String login(User user){
        //System.out.println(user);
        return userService.login(user);
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public String register(User user){
        return userService.register(user);
    }


    /**
     * 查找用户信息
     * @param token
     * @return
     */
    @GetMapping("/findUserInfo")
    public User findUserInfo(String token){
        if (JwtUtils.isJwtExpired(token)){
            return null;
        }
       else {
           return userService.getUserInfo(token);
        }
    }

    /**
     * 检查token状态
     * @param token
     * @return
     */
    @GetMapping("/checkTokenStatus")
    public boolean checkTokenStatus(String token){
        return JwtUtils.isJwtExpired(token);
    }
}
